package cn.itcast.bos.web.action.base;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.Servlet;
import javax.ws.rs.core.MediaType;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.output.FileWriterWithEncoding;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSONArray;

import cn.itcast.bos.constant.Constants;
import cn.itcast.bos.domain.page.PageBean;
import cn.itcast.bos.domain.take_delivery.Promotion;
import cn.itcast.bos.web.action.common.BaseAction;
import freemarker.core.Configurable;
import freemarker.template.Configuration;
import freemarker.template.Template;

@Controller
@Namespace("/")
@ParentPackage("struts-default")
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@SuppressWarnings("all")
public class PromotionAction extends BaseAction<Promotion> {
	// 分页展示
	@Action(value = "promotion_showPage")
	public void showPage() {
		// 响应到页面 中文乱码处理
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		// WebService 动态获取数据
		PageBean<Promotion> pageBean = WebClient
				.create(Constants.BOS_MANAGEMENT_URL + "/services/promotionService/showPage").query("page", page)
				.query("rows", rows).accept(MediaType.APPLICATION_JSON).get(PageBean.class);
		// 对象转json
		String json = JSONArray.toJSONString(pageBean);
		// 响应json到页面
		try {
			ServletActionContext.getResponse().getWriter().write(json);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// freemarker展示商品详情
	@Action(value = "promotion_detail")
	public void detail() throws Exception {
		// 判断文件是否存在,如果存在直接返回,不存在就访问数据库获取数据再通过freemarker合并输出
		// 获取存放静态页面的路径,根据id查询文件是否存在
		String rootPath = ServletActionContext.getServletContext().getRealPath("/freemarker");
		File promotionFile = new File(rootPath + "/" + model.getId() + ".html");
		System.out.println(promotionFile);
		System.out.println(promotionFile.exists());
		if (!promotionFile.exists()) {
			System.out.println("qqq");
			// 配置对象,获取模板文件目录
			Configuration configuration = new Configuration(Configuration.VERSION_2_3_22);
			configuration.setDirectoryForTemplateLoading(
					new File(ServletActionContext.getServletContext().getRealPath("/WEB-INF/freemarker_templates")));
			// 获取模板对象
			Template template = configuration.getTemplate("promotion_detail.ftl");
			// 动态获取数据
			Promotion promotion = WebClient
					.create(Constants.BOS_MANAGEMENT_URL + "/services/promotionService/findById/" + model.getId())
					.accept(MediaType.APPLICATION_JSON).get(Promotion.class);
			Map<String, Object> promotionMap = new HashMap<>();
			promotionMap.put("promotion", promotion);
			// 合并输出(字符流)
			template.process(promotionMap, new FileWriterWithEncoding(new File(promotionFile, ""), "utf-8"));
		}
		// 将文件copy到字节输出流 响应到页面
		ServletActionContext.getResponse().setCharacterEncoding("utf-8");
		FileUtils.copyFile(promotionFile, ServletActionContext.getResponse().getOutputStream());
	}
}
